package persistence;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.List;

import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

import domain.Game;

public class GamesXMLHandler extends XMLHandler<List<Game>> {

	public GamesXMLHandler() {
		
	}

	@Override
	public List<Game> readXML(String name, Unmarshaller unMarshaller) {
		
		List<Game> games = null;
		try {
			InputStream iStream = new FileInputStream(savedDataFolderName+"\\"+name+"\\"+name+"Games.xml");
			games = (List<Game>) unMarshaller.unmarshal(iStream);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JAXBException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return games;
		

		
	}

	@Override
	public void writeXML(List<Game> games, String name, Marshaller m) {
		
		OutputStream oStream = null;
		
		try {
			oStream = new FileOutputStream(savedDataFolderName+"\\"+name+"\\"+name+"Games.xml");
			m.marshal(games, oStream);
			
		} catch (FileNotFoundException e) {
			
			e.printStackTrace();
		} catch (JAXBException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally {
			try {
				oStream.close();
			} catch (Exception e) {
			}
		}
		
	}
}